//
//  LKVisitRecordViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/14.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKVisitRecordViewController.h"
#import "LKTestpageNameTitleCell.h"
#import "LKVisitModel.h"
@interface LKVisitRecordViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataListArray;
@property(nonatomic,assign)NSInteger currentPageIndex;
@property(nonatomic,assign)NSInteger MaxResultCount;
@end

@implementation LKVisitRecordViewController


-(LKBaseTableview *)tableView{
    if (!_tableView) {
        _tableView = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, 12 + NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 12)) style:UITableViewStyleGrouped registercellClasss:@[@"LKTestpageNameTitleCell"]];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        
    }
    return _tableView;
}
/**
 头部下拉刷新
 */
-(void)headerReloadData{
    self.currentPageIndex = 1;
    [self getAllVisitRecordWithCustomerName:@""];
}

/**
 尾部上拉加载更多
 */
-(void)footerReloadData{
    self.currentPageIndex++;
    [self getAllVisitRecordWithCustomerName:@""];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentPageIndex = 1;
    self.MaxResultCount = 10;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self getAllVisitRecordWithCustomerName:@""];
    self.tableView.estimatedRowHeight = 200;
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.title = @"拜访记录";
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"拜访记录";
    
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [btn setImage:[UIImage imageNamed:@"testpagesousuo"] forState:UIControlStateNormal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
//    [btn addTarget:self action:@selector(searchName:) forControlEvents:UIControlEventTouchUpInside];

}

/**
 搜索姓名查询

 */
-(void)searchName:(UIButton *)btn{
    WS(weakSelf)
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入姓名" message:@"" preferredStyle:UIAlertControllerStyleAlert];
//    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//
//    }];
//    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//
//    }]];
//    [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        UITextField *contentTf = alert.textFields.firstObject;
//        weakSelf.currentPageIndex = 1;
//        [weakSelf getAllVisitRecordWithCustomerName:contentTf.text];
//        NSLog(@"输入内容%@",contentTf.text);
//    }]];
//    [self presentViewController:alert animated:YES completion:nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataListArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKTestpageNameTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKTestpageNameTitleCell"];
    cell.timeLabel.hidden = NO;
    cell.visitModel = self.dataListArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0000000000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 12;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = cellHeaderBgColor;
    return view;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        LKVisitModel *model = self.dataListArray[indexPath.section];
        [self.dataListArray removeObjectAtIndex:indexPath.section];
        [self.tableView reloadData];
        [self deletVisitRecordInformationWithId:model.visitId];
    }
}
//删除拜访记录
-(void)deletVisitRecordInformationWithId:(NSString *)Id{
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:deleteVisitRecordUrl header:[LKUserInforTool header] parameters:@{@"Id":Id} progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [SVProgressHUD showInfoWithStatus:@"删除成功"];
    } failure:^(NSString * _Nullable errorMessage) {
        [SVProgressHUD showInfoWithStatus:@"删除失败"];
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

-(NSMutableArray *)dataListArray{
    if (!_dataListArray) {
        _dataListArray = [[NSMutableArray alloc] init];
    }
    return _dataListArray;
}
//获取拜访记录
-(void)getAllVisitRecordWithCustomerName:(NSString *)customerName{
    NSDictionary *parm  = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",self.currentPageIndex],@"PageIndex",[NSString stringWithFormat:@"%ld",self.MaxResultCount],@"MaxResultCount",customerName,@"CustomerName", nil];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:visitRecordUrl header:[LKUserInforTool header] parameters:parm progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (self.currentPageIndex == 1) {
            [self.dataListArray removeAllObjects];
        }
        NSArray *items = responseObject[@"result"][@"items"];
        for (NSDictionary *dict in items) {
            LKVisitModel *model = [LKVisitModel modelWithDictionary:dict];
            [self.dataListArray addObject:model];
        }
//        if (items.count < self.dataListArray.count) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        if (self.dataListArray.count == 0) {
            self.tableView.mj_footer.hidden = YES;
        }
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    NSLog(@"释放");
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
